package testview.zhen.com.myapplication.material_design

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import testview.zhen.com.myapplication.R

/**

 * @Author ldr
 * @Date 2020/1/5-20:21
 * @Email 326208323@qq.com
 */
class SampleAdapter(var context: Context) : RecyclerView.Adapter<SampleAdapter.SampleHolder>() {

    lateinit var dataList: ArrayList<String>
    fun setData(list: ArrayList<String>): Unit {
        dataList = list
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleHolder {
        return SampleHolder(LayoutInflater.from(context).inflate(R.layout.item_sample, parent, false))
    }

    override fun getItemCount(): Int {
        return if (dataList.isNullOrEmpty()) return 0 else dataList.size
    }

    override fun onBindViewHolder(holder: SampleHolder, position: Int) {
        if (!dataList.isNullOrEmpty()) {
            holder.mTv.text = dataList[position]
        }
    }

    class SampleHolder : RecyclerView.ViewHolder {
        lateinit var mTv: TextView

        constructor(itemView: View) : super(itemView) {
            mTv = itemView.findViewById(R.id.text)
        }
    }
}